<template>
	<section>


		<!--高级查询工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDictionaryType">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <el-table :data="rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              @row-dblclick="dataBackFillGoods"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="320" sortable>
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="320" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--批量删除工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :pageSize="pageSize"
                     :total="total"
                     :currentPage="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

    <!--数据字典编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">

        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" :min="0" :max="200"></el-input>
        </el-form-item>

        <el-form-item label="字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--明细编辑界面-->
    <el-dialog title="编辑" :visible.sync="mingxi" :close-on-click-modal="false">
      <el-form :model="mingxiForm" label-width="80px" :rules="editFormRules" ref="editForm">

        <el-form-item label="字典名" prop="name">
          <el-input v-model="mingxiForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="mingxi = false">取消</el-button>
        <el-button type="primary" @click.native="mingxiSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉框-->
    <el-drawer
        title="看我套娃"
        :visible.sync="emptable"
        direction="rtl"
        size="50%">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="CurrentChange">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="mingxiAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>

      <el-table :data="drows" highlight-current-row v-loading="listLoading"
                @row-dblclick="dataBackFillGoods"
                style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="sn" label="编号" width="220" sortable>
        </el-table-column>
        <el-table-column prop="name" label="数据字典名" width="220" sortable>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click="mingxiEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="mingxiDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

        <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="mingxiCurrentChange"
                       :pageSize="page"
                       :total="dtotal"
                       :currentPage="current"
                       style="float:right;">
        </el-pagination>
      </el-col>

    </el-drawer>

	</section>
</template>

<script>


	export default {
		data() {
			return {
			  //高级查询的字段
				filters: {
          keyword: ''
				},

        //数据字典的数据和总条数
				rows: [],
				total: 0,
        currentPage: 1,
        pageSize:5,
        editFormVisible: false,//数据字典编辑界面是否显示
        //数据字典数据
        editForm: {
          id: null,
          sn:'',
          name:''
        },


        //明细的数据和总条数
        drows:[],
        dtotal: 0,
        current: 1,
        page:5,
        mingxi:false, //明细编辑新增框是否显示
        num:null, //明细中的type-id字段，也就是数据字典中的id
        //明细数据
        mingxiForm:{
          id:null,
          name:'',
          types_id:null
        },


        listLoading: false, //是否开启忙等
        editLoading: false, //按钮忙等

				sels: [],//列表选中列(批量删除)

        emptable:false,//抽屉是否显示

        //校验规则
				editFormRules: {
				/*	name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]*/
				},

			}
		},

		methods: {
		  //得到明细数据的方法
		  getmingxitable(){
        let para = {
          num:this.num,
          currentPage: this.current,
          keyword: this.filters.keyword
        };
        this.listLoading = true;
        this.$http.post("/dictionarydetail",para)
            .then(result => {
              this.listLoading = false;
              result=result.data;
              this.dtotal=result.total;
              this.drows=result.data;
            })
            .catch(result=>{
              this.$message({
                message: '查询失败',
                type: 'error'
              });
            })
      },

		  //当前行双击事件
      dataBackFillGoods :function (row, column,event){
		    this.num=row.id;
        this.emptable=true;
        this.getmingxitable();
      },

			//获取数据字典列表  也是高级查询的方法
      getDictionaryType() {
        let para = {
          currentPage: this.currentPage,
          keyword: this.filters.keyword
        };
				this.listLoading = true;
        this.$http.post("/dictionary",para)
          .then(result => {
            this.listLoading = false;
            result=result.data;
            this.total=result.total;
            this.rows=result.data;
          })
          .catch(result=>{
            this.$message({
              message: '查询失败',
              type: 'error'
            });
          })
			},

			//显示数据字典编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				//数据回显
				this.editForm = Object.assign({}, row);
			},

      //显示数据字典新增界面
      handleAdd: function () {
        this.editFormVisible = true;
        this.editForm = {
          id: null,
          sn:'',
          name:''
        };
      },

			//数据字典提交触发事件
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  //开启忙等按钮
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$http.put("/dictionary",para)
                .then(result=>{
                  this.editFormVisible = false;
                  result=result.data;
                  if(result.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.currentPage=1;
                    this.getDictionaryType();
                  }else{
                    this.$message({
                      message: '程序出错，正在殴打程序员',
                      type: 'error'
                    });
                  }
                  this.editLoading = false;
                })
                .catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: '网络繁忙，请稍后再试',
                    type: 'error'
                  });
                })
						});
					}
				});
			},

      //数据字典删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {//确定
          this.listLoading = true;
          this.$http.delete("/dictionary/"+row.id)
              .then(result=>{
                result=result.data;
                if(result.success){
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '有明细，不可删除',
                    type: 'success'
                  });
                }
                this.currentPage=1;
                this.getDictionaryType();
                //不用关闭忙等框 getDictionaryType 调用这个方法的时候里面有关闭忙等框
              })
              .catch(result=>{
                this.currentPage=1;
                this.getDictionaryType();
              })
        }).catch(() => {
          //取消
        });
      },

      //数据字典点击页码触发事件
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getDictionaryType();
      },


      //明细新增
      mingxiAdd: function () {
        this.mingxi = true;
        this.mingxiForm = {
          id:null,
          name:'',
          types_id:this.num
        };
      },

      //明细修改
      mingxiEdit: function (index, row) {
        this.mingxi = true;
        //数据回显
        this.mingxiForm = Object.assign({}, row);
      },

      //明细编辑新增提交事件
      mingxiSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //开启忙等按钮
              this.editLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.mingxiForm);
              this.$http.put("/dictionarydetail",para)
                  .then(result=>{
                    this.mingxi = false;
                    result=result.data;
                    if(result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                      this.current=1;
                      this.getmingxitable();
                    }else{
                      this.$message({
                        message: '程序出错，正在殴打程序员',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                  })
                  .catch(result=>{
                    this.editLoading = false;
                    this.$message({
                      message: '网络繁忙，请稍后再试',
                      type: 'error'
                    });
                  })
            });
          }
        });
      },


      //明细删除删除
      mingxiDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {//确定
          this.listLoading = true;
          this.$http.delete("/dictionarydetail/"+row.id)
              .then(result=>{
                result=result.data;
                if(result.success){
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '程序出错，正在殴打程序员中',
                    type: 'success'
                  });
                }
                this.current=1;
                this.getmingxitable();
                //不用关闭忙等框 getDictionaryType 调用这个方法的时候里面有关闭忙等框
              })
              .catch(result=>{
                this.current=1;
                this.getmingxitable();
              })
        }).catch(() => {
          //取消
        });
      },

      //明细点击页码事件
      mingxiCurrentChange(val){
        this.current = val;
        this.getmingxitable();
      },

      //明细高级查询点击事件
      CurrentChange(){
        this.getmingxitable();
      },

      //给批量删除的数组赋值
			selsChange: function (sels) {
				this.sels = sels;
			},


			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					console.log(para);
          this.$http.patch("/emp",para)
            .then(result=>{
              this.currentPage=1;
              this.getDictionaryType();
            })
            .catch(result=>{
              this.currentPage=1;
              this.getDictionaryType();
            })
				}).catch(() => {

				});
			}
		},

    //钩子函数
		mounted() {
			this.getDictionaryType();
		}

	}

</script>

<style scoped>

</style>